Skip to content

fix(cli): register module preferences before set-prefs validation#10105

Merged
asheshv merged 1 commit into
masterfrom
fix/cli-set-prefs-regression
Jun 17, 2026
Merged

fix(cli): register module preferences before set-prefs validation#10105
asheshv merged 1 commit into
masterfrom
fix/cli-set-prefs-regression

Conversation

@asheshv

@asheshv asheshv commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Summary

  • PR Validate preference values set via the CLI (#7346) #10047 made Preferences.save_cli() validate values against the in-memory preference object before writing, but setup.py set-prefs only enters app.app_context() and never calls app.run_before_app_start(). Preferences.modules therefore stays empty, every save_cli lookup returns (False, "Module 'X' is no longer in use."), and the CLI reports a generic "Invalid value provided" for every preference — regardless of the actual value.
  • Trigger registration explicitly: app.PGADMIN_RUNTIME = False + app.run_before_app_start() before the save loop. run_before_app_start() enters both an app context and a test_request_context() internally, which satisfies registration callbacks that touch current_user. PGADMIN_RUNTIME is set to False so the full server-mode preference set (incl. keyboard shortcuts) is exposed to the CLI.
  • Surface the typed error message from save_cli per failed preference ("Could not set <pref>: Invalid value for integer option.") instead of the aggregate generic message — useful when batching multiple key=value pairs of mixed types in one command or via --input-file. save_pref now returns the (ok, msg) tuple straight from save_cli; its only caller is setup.py.

Test plan

  • python setup.py set-prefs <user> "browser:display:show_user_defined_templates=true" → saved
  • python setup.py set-prefs <user> "browser:display:browser_tree_state_save_interval=notanint" → "Could not set …: Invalid value for integer option."
  • python setup.py set-prefs <user> "browser:display:no_such_pref=foo" → "Preference(s) … not found."
  • python setup.py set-prefs <user> "browser:display:browser_tree_state_save_interval=5" → saved
  • pycodestyle clean on both files
  • GUI preferences dialog still saves correctly (unaffected — Preferences.save() path unchanged)

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced error messaging when setting preferences, now displaying specific error details for each preference rather than generic messages.
    • Improved preference registration handling for command-line interface operations.

PR #10047 made `Preferences.save_cli()` validate against the registered
preference object, but `setup.py set-prefs` only enters `app_context()`
and never calls `run_before_app_start()`, leaving `Preferences.modules`
empty. Every CLI write then fell into the "Module 'X' is no longer in
use." path and was reported as "Invalid value provided".

Trigger registration explicitly (run_before_app_start enters both
app and test_request contexts internally, satisfying registration
callbacks that touch current_user) and propagate the typed error
message from save_cli so users see the actual reason a value was
rejected.
@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 77938096-8ee6-46c8-a41a-916dd00de276

📥 Commits

Reviewing files that changed from the base of the PR and between 71c18fb and 1e4ac39.

📒 Files selected for processing (2)
  • web/pgadmin/preferences/__init__.py
  • web/setup.py

Walkthrough

save_pref now returns the raw result of Preferences.save_cli instead of normalizing it to a boolean. ManagePreferences.set_prefs adds explicit CLI lazy-registration (app.PGADMIN_RUNTIME = False + app.run_before_app_start()), collects save failures as (key, error_message) tuples, and prints per-preference error messages on failure.

Changes

CLI Preference Save Flow

Layer / File(s) Summary
save_pref return value passthrough
web/pgadmin/preferences/__init__.py
Removes the res, _ = ... unpacking and boolean normalization; save_pref now returns whatever Preferences.save_cli(...) returns directly.
set_prefs CLI init, error tuple collection, and per-error output
web/setup.py
Adds app.PGADMIN_RUNTIME = False and app.run_before_app_start() before preference saving to trigger lazy module registration. On save_pref failure, records (preference_key, error_message) tuples instead of only the key. Updates invalid-value reporting to print "Could not set <key>: <error>" per failure.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • pgadmin-org/pgadmin4#10047: Directly modifies the same CLI preference-setting code path in web/setup.py's ManagePreferences.set_prefs and the save_pref/Preferences.save_cli result handling.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: registering module preferences before CLI set-prefs validation, which directly addresses the core issue fixed in this PR.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/cli-set-prefs-regression

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@asheshv asheshv requested a review from hiteshjambhale June 17, 2026 07:24
@asheshv asheshv merged commit 660ecf2 into master Jun 17, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant